import 'package:flutter/material.dart';
import 'package:get_storage/get_storage.dart';
import 'package:get/get.dart';
import 'dart:io';
import 'package:zhaoxiansheng_app/controller/ThemeController.dart';
import 'package:zhaoxiansheng_app/controller/LoginController.dart';
class Person extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return PersonState();
  }
}

class PersonState extends State<Person> {
  var storage = GetStorage();
  Rx<Person?> userInfo = Rx(null);
  Rx<String> imagePath = Rx<String>("");
  final themeController = Get.find<ThemeController>();
  final loginController = Get.find<LoginController>();
  Rx<Map<String,dynamic>?> map = Rx(null);
  @override
  void initState() {
    super.initState();
    storage.write("userName", "zky");
     map.value = storage.read("userInfo");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Padding(
        padding: EdgeInsetsGeometry.symmetric(vertical: 30),
        child: Column(
          children: [
            GestureDetector(
              onTap: () {
                //没有个人信息跳转到登陆页面
                if(map.value==null){
                  Get.toNamed("/login");
                }
                else {
                  Get.defaultDialog(
                    title: "提示",
                    middleText: "确定要退出登陆吗",
                    textCancel: "取消",
                    textConfirm: "确认",
                    confirmTextColor: Colors.white,
                    onConfirm: (){
                      map.value  = null;
                      storage.remove('userInfo');
                      Get.back();
                      Get.snackbar("提示", "已退出登陆");
                    }
                  );
                }

              },

              child: ListTile(
                leading: Obx(() {
                  if (map.value!=null) {
                    return CircleAvatar(
                      backgroundImage: FileImage(File(map.value!['imageUrl'])),
                    );
                  } else {
                    return CircleAvatar(
                      backgroundColor: Colors.blue,
                      child: Text("登录"),
                    );
                  }
                }),
                title: Obx(() {
                  if (map.value==null) {
                    return Text("登录/注册");
                  } else {
                    return Text("欢迎您:${loginController.userName}");
                  }
                }),

                subtitle: Obx(() {
                  if (map.value==null) {
                    return Text(
                      "主人还没有登陆哦",
                      style: TextStyle(color: Colors.grey, fontSize: 12),
                    );
                  }
                  return Text(
                    "${loginController.userName},感谢您使用我的APP",
                    style: TextStyle(color: Colors.grey, fontSize: 12),
                  );
                }),

                trailing: Icon(
                  Icons.chevron_right,
                  color: Colors.grey,
                ), // 向右的箭头
              ),
            ),

            SizedBox(height: 35),
            Row(
              children: [
                Expanded(
                  child: Container(
                    child: Column(
                      children: [Icon(Icons.favorite, size: 35), Text("我的收藏")],
                    ),
                  ),
                ),
                Expanded(
                  child: Container(
                    child: Column(
                      children: [Icon(Icons.history, size: 35), Text("历史记录")],
                    ),
                  ),
                ),
                Expanded(
                  child: Container(
                    child: Column(
                      children: [Icon(Icons.file_copy, size: 35), Text("离线缓存")],
                    ),
                  ),
                ),
                Expanded(
                  child: Container(
                    child: Column(
                      children: [Icon(Icons.email, size: 35), Text("我的消息")],
                    ),
                  ),
                ),
              ],
            ),
            SizedBox(height: 20),
            Container(
              child: ListTile(
                leading: SizedBox(
                  width: 30,
                  height: 50,
                  child: Image.asset("assests/1.webp", fit: BoxFit.cover),
                ),
                title: Text("夏日口袋 Summer Pocket"),
                subtitle: Text(
                  "已看到21集",
                  style: TextStyle(color: Colors.grey, fontSize: 15),
                ),
                trailing: Row(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text("继续追番", style: TextStyle(fontSize: 15)),
                    Icon(Icons.chevron_right),
                  ],
                ),
              ),
            ),
            SizedBox(height: 20),
            Row(
              mainAxisAlignment: MainAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: [
                Expanded(
                  flex: 1,
                  child: Center(
                    child: ListTile(
                      leading: Icon(Icons.assessment),
                      title: Text("入站问答"),
                      subtitle: Text(
                        "通过问答才有发言权哦",
                        style: TextStyle(fontSize: 10),
                      ),
                    ),
                  ),
                ),
                Expanded(
                  child: Center(
                    child: GestureDetector(
                      child: ListTile(
                        leading: Icon(Icons.palette),
                        title: Obx(() {
                          return Text(
                            themeController.isDark.value ? "切换到亮色" : "切换到暗色",
                          );
                        }),
                        subtitle: Text(
                          "挑选一款心仪的配色",
                          style: TextStyle(fontSize: 10),
                        ),
                      ),
                      onTap: () {
                        themeController.toggleTheme();
                      },
                    ),
                  ),
                ),
              ],
            ),

            Card(
              child: Column(
                children: [
                  ListTile(
                    leading: Icon(Icons.feedback),
                    title: Text("意见反馈"),
                    trailing: Icon(Icons.chevron_right),
                  ),
                  ListTile(
                    leading: Icon(Icons.share),
                    title: Text("分享应用"),
                    trailing: Icon(Icons.chevron_right),
                  ),
                ],
              ),
            ),
            Card(
              child: Column(
                children: [
                  ListTile(
                    leading: Icon(Icons.warning_amber),
                    title: Text("免责声明"),
                    trailing: Icon(Icons.chevron_right),
                  ),
                  ListTile(
                    leading: Icon(Icons.system_update_alt),
                    title: Text("检测更新"),
                    trailing: Icon(Icons.chevron_right),
                  ),
                  ListTile(
                    leading: Icon(Icons.restore),
                    title: Text("数据恢复"),
                    trailing: Icon(Icons.chevron_right),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
